package Ms.service.Impl;


import Ms.dao.ProjectMapper;
import Ms.entity.ProjectQuery;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import Ms.entity.Project;
import Ms.entity.enums.ProjectStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import Ms.service.ProjectService;
import Ms.service.SysOperationLogService;
import Ms.utils.Vo.ProjectVO;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper; // 添加这行代码
    @Autowired
    private SysOperationLogService logService;

    @Override
    public Page<ProjectVO> getProjectPage(ProjectQuery query) {
        // 分页参数：current=页码（从 1 开始），size=每页数量
        Page<Project> projectPage = new Page<>(query.getPage(), query.getSize());
        // 使用 BaseMapper 提供的 selectPage 方法
        Page<Project> resultPage = projectMapper.selectPage(
                projectPage,
                Wrappers.<Project>lambdaQuery() // 显式指定实体类类型
                        .eq(query.getStatus() != null, Project::getStatus, query.getStatus())
                        .orderByDesc(Project::getCreateTime)
        );
        // 转换为 VO 分页对象
        return (Page<ProjectVO>) resultPage.convert(this::convertToVO);
    }

    @Override
    @Transactional
    public boolean deleteProject(Long projectId) {
        Project project = projectMapper.selectById(projectId);
        if (project == null) {
            System.out.println("-----------------");
//            throw new BusinessException("项目不存在");
        }
        if (project.getStatus() == ProjectStatus.APPROVED) {
            System.out.println("-----------------");
//            throw new BusinessException("已审核项目不可删除");
        }
        // 记录操作日志
        logService.logDeleteOperation(StpUtil.getLoginIdAsLong(), projectId);
        return projectMapper.deleteByIdWithLogic(projectId) > 0;
    }


    private ProjectVO convertToVO(Project project) {
        // 转换逻辑
        return null;
    }
    @Override
    public Project getById(Long projectId) {
        return projectMapper.selectById(projectId);
    }

    @Override
    public Long countProjects() {
        return projectMapper.selectCount(null); // 或根据业务条件统计
    }

    @Override
    @Transactional
    public boolean logicDelete(Long projectId) {
        return projectMapper.deleteByIdWithLogic(projectId) > 0;
    }
}
